package com.fr.chart.core;

import com.fr.base.BaseUtils;
import com.fr.base.ColumnRow;
import com.fr.base.core.DateUtils;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.report.cellElement.TextFormat;
import com.fr.util.Base64;
import com.fr.util.Utils;
import com.fr.web.core.Attachment;
import com.fr.web.core.service.AttachmentService;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D.Double;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.geom.Rectangle2D.Double;
import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ChartUtils
{
  public static double fixDoubleValue(double paramDouble)
  {
    long l1 = Double.doubleToRawLongBits(paramDouble);
    int i = (l1 >> 63 == 0L) ? 1 : -1;
    int j = (int)(l1 >> 52 & 0x7FF);
    long l2 = (j == 0) ? (l1 & 0xFFFFFFFF) << 1 : l1 & 0xFFFFFFFF | 0x0;
    return (i * l2 * Math.pow(2.0D, j - 1075.0D));
  }

  public static Object getObject(int paramInt, Object[] paramArrayOfObject)
  {
    if ((paramArrayOfObject == null) || (paramArrayOfObject.length <= 0))
      return null;
    int i = paramInt % paramArrayOfObject.length;
    return paramArrayOfObject[i];
  }

  public static void draw3DRectangle(Graphics2D paramGraphics2D, double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, Color paramColor, boolean paramBoolean1, boolean paramBoolean2)
  {
    if (paramColor == null)
      paramColor = ChartCoreConstants.CHART_COLOR_ARRAY[0];
    Color localColor = paramColor.brighter();
    if (paramBoolean2)
    {
      if (paramBoolean1)
      {
        paramGraphics2D.setComposite(AlphaComposite.getInstance(3, 0.7F));
        paramGraphics2D.setPaint(new GradientPaint((float)paramDouble1, (float)paramDouble2, paramColor, (float)paramDouble1, (float)(paramDouble2 + paramDouble4 / 3.0D), localColor));
        GraphHelper.fillRect(paramGraphics2D, paramDouble1, paramDouble2, paramDouble3, paramDouble4 / 3.0D);
        paramGraphics2D.setComposite(AlphaComposite.getInstance(3, 0.7F));
        paramGraphics2D.setPaint(new GradientPaint((float)paramDouble1, (float)(paramDouble2 + paramDouble4 / 3.0D), localColor, (float)paramDouble1, (float)(paramDouble2 + paramDouble4), paramColor));
        GraphHelper.fillRect(paramGraphics2D, paramDouble1, paramDouble2 + paramDouble4 / 3.0D, paramDouble3, 2.0D * paramDouble4 / 3.0D);
      }
      else
      {
        paramGraphics2D.setPaint(new GradientPaint((float)paramDouble1, (float)paramDouble2, paramColor, (float)paramDouble1, (float)(paramDouble2 + paramDouble4 / 3.0D + 1.0D), localColor));
        GraphHelper.fillRect(paramGraphics2D, paramDouble1, paramDouble2, paramDouble3, paramDouble4 / 3.0D + 1.0D);
        paramGraphics2D.setComposite(AlphaComposite.getInstance(3));
        paramGraphics2D.setPaint(new GradientPaint((float)paramDouble1, (float)(paramDouble2 + paramDouble4 / 3.0D), localColor, (float)paramDouble1, (float)(paramDouble2 + paramDouble4), paramColor));
        GraphHelper.fillRect(paramGraphics2D, paramDouble1, paramDouble2 + paramDouble4 / 3.0D, paramDouble3, 2.0D * paramDouble4 / 3.0D);
      }
    }
    else if (paramBoolean1)
    {
      paramGraphics2D.setComposite(AlphaComposite.getInstance(3, 0.7F));
      paramGraphics2D.setPaint(new GradientPaint((float)paramDouble1, (float)paramDouble2, paramColor, (float)(paramDouble1 + paramDouble3 / 3.0D), (float)paramDouble2, localColor));
      GraphHelper.fillRect(paramGraphics2D, paramDouble1, paramDouble2, paramDouble3 / 3.0D, paramDouble4);
      paramGraphics2D.setComposite(AlphaComposite.getInstance(3, 0.7F));
      paramGraphics2D.setPaint(new GradientPaint((float)(paramDouble1 + paramDouble3 / 3.0D), (float)paramDouble2, localColor, (float)(paramDouble1 + paramDouble3), (float)paramDouble2, paramColor));
      GraphHelper.fillRect(paramGraphics2D, paramDouble1 + paramDouble3 / 3.0D, paramDouble2, 2.0D * paramDouble3 / 3.0D, paramDouble4);
    }
    else
    {
      paramGraphics2D.setPaint(new GradientPaint((float)paramDouble1, (float)paramDouble2, paramColor, (float)(paramDouble1 + paramDouble3 / 3.0D + 1.0D), (float)paramDouble2, localColor));
      GraphHelper.fillRect(paramGraphics2D, paramDouble1, paramDouble2, paramDouble3 / 3.0D + 1.0D, paramDouble4);
      paramGraphics2D.setComposite(AlphaComposite.getInstance(3));
      paramGraphics2D.setPaint(new GradientPaint((float)(paramDouble1 + paramDouble3 / 3.0D), (float)paramDouble2, localColor, (float)(paramDouble1 + paramDouble3), (float)paramDouble2, paramColor));
      GraphHelper.fillRect(paramGraphics2D, paramDouble1 + paramDouble3 / 3.0D, paramDouble2, 2.0D * paramDouble3 / 3.0D, paramDouble4);
    }
  }

  public static void draw3DEllipse(Graphics2D paramGraphics2D, double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, Color paramColor, boolean paramBoolean1, boolean paramBoolean2)
  {
    if (paramColor == null)
      paramColor = ChartCoreConstants.CHART_COLOR_ARRAY[0];
    Rectangle2D.Double localDouble = new Rectangle2D.Double(paramDouble1, paramDouble2 + paramDouble4 / 2.0D, paramDouble3, paramDouble4 / 2.0D);
    Area localArea1 = new Area(localDouble);
    Ellipse2D.Double localDouble1 = new Ellipse2D.Double(paramDouble1, paramDouble2, paramDouble3, paramDouble4);
    Area localArea2 = new Area(localDouble1);
    localArea2.intersect(localArea1);
    if (paramBoolean2)
      return;
    if (paramBoolean1)
    {
      paramGraphics2D.setPaint(paramColor);
      paramGraphics2D.setComposite(AlphaComposite.getInstance(3, 0.7F));
      paramGraphics2D.fill(new Ellipse2D.Double(paramDouble1, paramDouble2, paramDouble3, paramDouble4));
    }
    else
    {
      paramGraphics2D.setPaint(paramColor);
      paramGraphics2D.fill(new Ellipse2D.Double(paramDouble1, paramDouble2, paramDouble3, paramDouble4));
    }
  }

  public static void draw3DCylinder(Graphics2D paramGraphics2D, double paramDouble1, double paramDouble2, double paramDouble3, double paramDouble4, Color paramColor, boolean paramBoolean1, boolean paramBoolean2)
  {
    if (paramColor == null)
      paramColor = ChartCoreConstants.CHART_COLOR_ARRAY[0];
    draw3DEllipse(paramGraphics2D, paramDouble1, paramDouble2 + paramDouble4 - paramDouble3 / 8.0D, paramDouble3, paramDouble3 / 4.0D, paramColor, false, paramBoolean2);
    paramGraphics2D.setComposite(AlphaComposite.getInstance(3));
    draw3DRectangle(paramGraphics2D, paramDouble1, paramDouble2, paramDouble3, paramDouble4, paramColor, false, paramBoolean2);
    paramGraphics2D.setPaint(paramColor);
    paramGraphics2D.setComposite(AlphaComposite.getInstance(3));
    paramGraphics2D.fill(new Ellipse2D.Double(paramDouble1, paramDouble2 - paramDouble3 / 8.0D, paramDouble3, paramDouble3 / 4.0D));
  }

  public static ColumnRow[] convertStringToColumnRow(String paramString)
  {
    int i;
    if ((paramString == null) || (paramString.trim().length() == 0))
      return new ColumnRow[0];
    ArrayList localArrayList = new ArrayList();
    String[] arrayOfString = Utils.splitString(paramString.trim(), ',');
    if ((arrayOfString != null) && (arrayOfString.length > 0))
      for (i = 0; i < arrayOfString.length; ++i)
        dealStringWithOutComma(localArrayList, arrayOfString[i]);
    return ((ColumnRow[])localArrayList.toArray(new ColumnRow[localArrayList.size()]));
  }

  private static void dealStringWithOutComma(List paramList, String paramString)
  {
    String[] arrayOfString = paramString.trim().split(":");
    if ((arrayOfString != null) && (arrayOfString.length > 1))
    {
      int i = -1;
      int j = -1;
      int k = -1;
      int l = -1;
      for (int i1 = 0; i1 < arrayOfString.length; ++i1)
      {
        localColumnRow = BaseUtils.convertCellStringToColumnRow(arrayOfString[i1].trim());
        if (localColumnRow.getColumn() != -1)
        {
          if (localColumnRow.getRow() == -1)
            break label149:
          if (i == -1)
          {
            i = localColumnRow.getColumn();
            j = localColumnRow.getColumn();
            k = localColumnRow.getRow();
            l = localColumnRow.getRow();
          }
          else
          {
            i = Math.min(i, localColumnRow.getColumn());
            j = Math.max(j, localColumnRow.getColumn());
            k = Math.min(k, localColumnRow.getRow());
            l = Math.max(l, localColumnRow.getRow());
          }
        }
      }
      label149: i1 = k;
      while (true)
      {
        if (i1 > l)
          return;
        for (int i2 = i; i2 <= j; ++i2)
        {
          localColumnRow = ColumnRow.valueOf(i2, i1);
          if (!(paramList.contains(localColumnRow)))
            paramList.add(paramList.size(), localColumnRow);
        }
        ++i1;
      }
    }
    ColumnRow localColumnRow = BaseUtils.convertCellStringToColumnRow(paramString.trim());
    if (!(paramList.contains(localColumnRow)))
      paramList.add(paramList.size(), localColumnRow);
  }

  public static Point2D intersectsPoint(Point2D paramPoint2D1, Point2D paramPoint2D2, Point2D paramPoint2D3, Point2D paramPoint2D4)
  {
    double d1 = (paramPoint2D4.getY() - paramPoint2D3.getY()) * (paramPoint2D2.getX() - paramPoint2D1.getX()) - (paramPoint2D2.getY() - paramPoint2D1.getY()) * (paramPoint2D4.getX() - paramPoint2D3.getX());
    if (Math.abs(d1) < 0.01D)
      return null;
    double d2 = (paramPoint2D4.getY() - paramPoint2D3.getY()) * paramPoint2D3.getX() - (paramPoint2D4.getX() - paramPoint2D3.getX()) * paramPoint2D3.getY();
    double d3 = (paramPoint2D2.getY() - paramPoint2D1.getY()) * paramPoint2D1.getX() - (paramPoint2D2.getX() - paramPoint2D1.getX()) * paramPoint2D1.getY();
    double d4 = ((paramPoint2D2.getX() - paramPoint2D1.getX()) * d2 - (paramPoint2D4.getX() - paramPoint2D3.getX()) * d3) / d1;
    double d5 = ((paramPoint2D2.getY() - paramPoint2D1.getY()) * d2 - (paramPoint2D4.getY() - paramPoint2D3.getY()) * d3) / d1;
    return new Point2D.Double(d4, d5);
  }

  public static Point2D intersectsPoint(Line2D paramLine2D1, Line2D paramLine2D2)
  {
    Point2D localPoint2D1 = paramLine2D1.getP1();
    Point2D localPoint2D2 = paramLine2D1.getP2();
    Point2D localPoint2D3 = paramLine2D2.getP1();
    Point2D localPoint2D4 = paramLine2D2.getP2();
    return intersectsPoint(localPoint2D1, localPoint2D2, localPoint2D3, localPoint2D4);
  }

  public static String color2JS(Color paramColor)
  {
    if (paramColor == null)
      return null;
    return "rgb(" + paramColor.getRed() + "," + paramColor.getGreen() + "," + paramColor.getBlue() + ")";
  }

  public static String format2JS(Format paramFormat)
  {
    if (paramFormat == null)
      return null;
    String str = "";
    if (paramFormat instanceof SimpleDateFormat)
      str = "D" + ((SimpleDateFormat)paramFormat).toPattern().trim();
    else if (paramFormat instanceof DecimalFormat)
      str = ((DecimalFormat)paramFormat).toPattern().trim();
    else if (paramFormat instanceof TextFormat)
      str = "T";
    return str;
  }

  public static String fontStyle2JS(int paramInt)
  {
    String str = "";
    if (paramInt == 0)
      str = "";
    else if (paramInt == 1)
      str = "bold";
    else if (paramInt == 2)
      str = "italic";
    else if (paramInt == 3)
      str = "bold italic";
    return str;
  }

  public static void image2JS(JSONObject paramJSONObject, Image paramImage)
    throws JSONException
  {
    if (paramImage == null)
      return;
    paramJSONObject.put("iesrc", AttachmentService.addAttachment(paramImage).getID());
    paramJSONObject.put("src", "data:image/png;base64," + Base64.encode(paramImage, "png"));
  }

  public static double date2Int(Date paramDate)
  {
    if (paramDate == null)
      paramDate = new Date();
    Date localDate = new Date("1900/01/01");
    return getTwoDay(paramDate, localDate);
  }

  public static Object object2DateDouble(Object paramObject)
  {
    String str = paramObject.toString();
    Number localNumber = Utils.string2Number(str);
    if (localNumber != null)
      return localNumber;
    Date localDate = DateUtils.string2Date(paramObject.toString(), true);
    if (localDate != null)
      paramObject = new Double(date2Int(localDate));
    return paramObject;
  }

  public static Date int2Date(int paramInt)
  {
    Date localDate = new Date("1900/01/01");
    long l = paramInt * 86400000L + localDate.getTime();
    return new Date(l);
  }

  public static double getTwoDay(Date paramDate1, Date paramDate2)
  {
    long l = (paramDate1.getTime() - paramDate2.getTime()) / 86400000L;
    return l;
  }
}